Public Class CUser
    Inherits BOBase

    Protected m_State As UserDS
    Protected m_DA As Users

#Region "   Inherits Implementation"
    Sub New()
        m_State = New UserDS
        m_DA = New Users
    End Sub

    Public Overrides ReadOnly Property DA() As DABase
        Get
            If m_DA Is Nothing Then
                m_DA = New Users
            End If

            Return m_DA
        End Get
    End Property

    Public Overrides Property ObjectState() As DataSet
        Get
            Return m_State
        End Get
        Set(ByVal Value As DataSet)
            Try
                m_State = CType(Value, UserDS)
            Catch ex As InvalidCastException
                Throw New ApplicationException("Invalid USER Dataset!", ex)
            End Try
        End Set
    End Property

    Public Overrides Sub Init()
        Dim dr As UserDS.USERRow = m_State.USER.NewUSERRow
        With dr
            .NAME = String.Empty
            .LOGIN_NAME = String.Empty
            .PASSWORD = String.Empty
            .EMAIL = String.Empty
            .USER_TYPE = Nothing
            .SetCOUNTRYNull()
            .SetCUSTOMER_CODENull()
            .SetBAR_CODENull()
            .SetIS_ADMINNull()
            .SetIS_ENABLEDNull()
            .SetCREATED_DATENull()
            .SetCREATED_BYNull()
            .SetUPDATED_DATENull()
            .SetUPDATED_BYNull()
            .SetSORT_REFERENCE1Null()
            .SetSORT_REFERENCE2Null()
            .SetDOWNLOADABLE_CSVNull()
        End With
        m_State.USER.Rows.Add(dr)
    End Sub
#End Region

#Region "   Data Properties"
    Protected ReadOnly Property DataRow() As UserDS.USERRow
        Get
            Return CType(Me.Row, UserDS.USERRow)
        End Get
    End Property

    Public ReadOnly Property UsIdPk() As System.Int32
        Get
            Return Me.DataRow.US_ID_PK
        End Get
    End Property

    Public Property Name() As System.String
        Get
            Return Me.DataRow.NAME
        End Get
        Set(ByVal Value As System.String)
            Me.DataRow.NAME = Value
        End Set
    End Property

    Public Property LoginName() As System.String
        Get
            Return Me.DataRow.LOGIN_NAME
        End Get
        Set(ByVal Value As System.String)
            Me.DataRow.LOGIN_NAME = Value
        End Set
    End Property

    Public Property Password() As System.String
        Get
            Return Me.DataRow.PASSWORD
        End Get
        Set(ByVal Value As System.String)
            Me.DataRow.PASSWORD = Value
        End Set
    End Property

    Public Property Email() As System.String
        Get
            Return Me.DataRow.EMAIL
        End Get
        Set(ByVal Value As System.String)
            Me.DataRow.EMAIL = Value
        End Set
    End Property

    Public Property UserType() As System.Byte
        Get
            Return Me.DataRow.USER_TYPE
        End Get
        Set(ByVal Value As System.Byte)
            Me.DataRow.USER_TYPE = Value
        End Set
    End Property

    Public Property Country() As System.String
        Get
            If Me.DataRow.IsCOUNTRYNull Then
                Return Nothing
            Else
                Return Me.DataRow.COUNTRY
            End If
        End Get
        Set(ByVal Value As System.String)
            If Value = Nothing Then
                Me.DataRow.SetCOUNTRYNull()
            Else
                Me.DataRow.COUNTRY = Value
            End If
        End Set
    End Property

    Public Property CustomerCode() As System.String
        Get
            If Me.DataRow.IsCUSTOMER_CODENull Then
                Return Nothing
            Else
                Return Me.DataRow.CUSTOMER_CODE
            End If
        End Get
        Set(ByVal Value As System.String)
            If Value = Nothing Then
                Me.DataRow.SetCUSTOMER_CODENull()
            Else
                Me.DataRow.CUSTOMER_CODE = Value
            End If
        End Set
    End Property

    Public Property BarCode() As System.String
        Get
            If Me.DataRow.IsBAR_CODENull Then
                Return Nothing
            Else
                Return Me.DataRow.BAR_CODE
            End If
        End Get
        Set(ByVal Value As System.String)
            If Value = Nothing Then
                Me.DataRow.SetBAR_CODENull()
            Else
                Me.DataRow.BAR_CODE = Value
            End If
        End Set
    End Property

    Public Property IsAdmin() As System.Boolean
        Get
            If Me.DataRow.IsIS_ADMINNull Then
                Return Nothing
            Else
                Return Me.DataRow.IS_ADMIN
            End If
        End Get
        Set(ByVal Value As System.Boolean)
            If Value = Nothing Then
                Me.DataRow.SetIS_ADMINNull()
            Else
                Me.DataRow.IS_ADMIN = Value
            End If
        End Set
    End Property

    Public Property IsEnabled() As System.Boolean
        Get
            If Me.DataRow.IsIS_ENABLEDNull Then
                Return Nothing
            Else
                Return Me.DataRow.IS_ENABLED
            End If
        End Get
        Set(ByVal Value As System.Boolean)
            If Value = Nothing Then
                Me.DataRow.SetIS_ENABLEDNull()
            Else
                Me.DataRow.IS_ENABLED = Value
            End If
        End Set
    End Property

    Public Property CreatedDate() As System.DateTime
        Get
            If Me.DataRow.IsCREATED_DATENull Then
                Return Nothing
            Else
                Return Me.DataRow.CREATED_DATE
            End If
        End Get
        Set(ByVal Value As System.DateTime)
            If Value = Nothing Then
                Me.DataRow.SetCREATED_DATENull()
            Else
                Me.DataRow.CREATED_DATE = Value
            End If
        End Set
    End Property

    Public Property CreatedBy() As System.Int32
        Get
            If Me.DataRow.IsCREATED_BYNull Then
                Return Nothing
            Else
                Return Me.DataRow.CREATED_BY
            End If
        End Get
        Set(ByVal Value As System.Int32)
            If Value = Nothing Then
                Me.DataRow.SetCREATED_BYNull()
            Else
                Me.DataRow.CREATED_BY = Value
            End If
        End Set
    End Property

    Public Property UpdatedDate() As System.DateTime
        Get
            If Me.DataRow.IsUPDATED_DATENull Then
                Return Nothing
            Else
                Return Me.DataRow.UPDATED_DATE
            End If
        End Get
        Set(ByVal Value As System.DateTime)
            If Value = Nothing Then
                Me.DataRow.SetUPDATED_DATENull()
            Else
                Me.DataRow.UPDATED_DATE = Value
            End If
        End Set
    End Property

    Public Property UpdatedBy() As System.Int32
        Get
            If Me.DataRow.IsUPDATED_BYNull Then
                Return Nothing
            Else
                Return Me.DataRow.UPDATED_BY
            End If
        End Get
        Set(ByVal Value As System.Int32)
            If Value = Nothing Then
                Me.DataRow.SetUPDATED_BYNull()
            Else
                Me.DataRow.UPDATED_BY = Value
            End If
        End Set
    End Property

    Public Property SortReference1() As System.String
        Get
            If Me.DataRow.IsSORT_REFERENCE1Null Then
                Return Nothing
            Else
                Return Me.DataRow.SORT_REFERENCE1
            End If
        End Get
        Set(ByVal Value As System.String)
            If Value = Nothing Then
                Me.DataRow.SetSORT_REFERENCE1Null()
            Else
                Me.DataRow.SORT_REFERENCE1 = Value
            End If
        End Set
    End Property

    Public Property SortReference2() As System.String
        Get
            If Me.DataRow.IsSORT_REFERENCE2Null Then
                Return Nothing
            Else
                Return Me.DataRow.SORT_REFERENCE2
            End If
        End Get
        Set(ByVal Value As System.String)
            If Value = Nothing Then
                Me.DataRow.SetSORT_REFERENCE2Null()
            Else
                Me.DataRow.SORT_REFERENCE2 = Value
            End If
        End Set
    End Property

    Public Property DownloadableCSV() As System.Boolean
        Get
            If Me.DataRow.IsDOWNLOADABLE_CSVNull Then
                Return Nothing
            Else
                Return Me.DataRow.DOWNLOADABLE_CSV
            End If
        End Get
        Set(ByVal Value As System.Boolean)
            If Value = Nothing Then
                Me.DataRow.SetDOWNLOADABLE_CSVNull()
            Else
                Me.DataRow.DOWNLOADABLE_CSV = Value
            End If
        End Set
    End Property

#End Region

#Region "   Utilities function"
    Public Shared Function GetUserList(ByVal userType As Byte) As DataTable
        Dim ds As New DataSet
        ds = DataHelper.ExecuteDataSet2("GetUserList", userType)
        Return ds.Tables(0)
    End Function

    Public Shared Function GetUserList() As DataTable
        Dim ds As New DataSet
        ds = DataHelper.ExecuteDataSet2("GetAllUsers")
        Return ds.Tables(0)
    End Function

    Public Function GetUserListByUsertype(ByVal userType As Byte, ByVal startRecord As Integer, ByVal maxRecords As Integer) As DataSet
        Return Me.m_DA.GetUserListByUsertype(userType, startRecord, maxRecords)
    End Function

    Public Shared Function GetName(ByVal userID As Integer) As String
        If userID > 0 Then
            Dim objCus As New CUser
            Try
                objCus.Load(userID)
            Catch ex As Exception
                Throw New ApplicationException("Can not load name", ex)
            End Try

            Return objCus.Name
        Else
            Return Nothing
        End If
    End Function

    Public Shared Function GetUserByName(ByVal username As String) As DataTable
        Dim ds As New DataSet
        ds = DataHelper.ExecuteDataSet2("GetUserByName", username)
        Return ds.Tables(0)
    End Function

    Public Shared Function GetOldPassword(ByVal userID As Integer) As String
        Dim ds As New DataSet
        ds = DataHelper.ExecuteDataSet("GetOldPassword", userID)
        If ds.Tables(0).Rows.Count <= 0 Then
            Return ""
        Else
            Return CStr(ds.Tables(0).Rows(0).Item(0))
        End If
    End Function

    Public Shared Function Login(ByVal username As String, ByVal pass As String) As Integer
        Dim dt As New DataTable
        dt = GetUserByName(username)

        ' Username wrong
        If dt.Rows.Count <= 0 Then
            Throw New ApplicationException("Invalid user name or password")
        End If

        'Password wrong
        If CStr(dt.Rows(0).Item(3)) <> CStr(pass) Then
            Throw New ApplicationException("Invalid user name or password")
        End If

        Return CInt(dt.Rows(0).Item(0))
    End Function

    Public Shared Function UpdatePassword(ByVal userID As Integer, ByVal pass As String) As Integer
        Try
            Return DataHelper.ExecuteNonQuery("UpdatePassword", userID, pass)
        Catch ex As Exception
            Throw New ApplicationException("Your Password has been changed UNsuccessfully!")
        End Try
    End Function

    Public Shared Function GenerateChangePasswordGUID(ByVal sEmailAddr As String) As String
        Dim sGuid As String = CType(DataHelper.ExecuteDataSet2("User_GenerateChangePasswordGUID", sEmailAddr).Tables(0).Rows(0)(0), Guid).ToString
        Return sGuid
    End Function

    Public Shared Function GetUserByChangePasswordRequestId(ByVal guid As String) As DataTable
        Dim ds As New UserDS

        Try
            'ds = DataHelper.ExecuteDataSet2("User_SelectByChangePasswordRequestGUID", New System.Guid(guid))
            DataHelper.FillDataset(ds, ds.USER.TableName, New SqlConnection(Common.GetConnectionString), "User_SelectByChangePasswordRequestGUID", New System.Guid(guid))
        Catch ex As Exception
            Throw New ApplicationException("Invalid Change Password Request!")
        End Try

        If ds.Tables(0).Rows.Count <= 0 Then
            Throw New ApplicationException("Invalid Change Password Request!")
        End If

        Return ds.Tables(0)
    End Function

    Public Shared Function CheckDuplicateData(ByVal strCustomerCode As String, ByVal strEmail As String, ByVal strLoginName As String) As DataTable
        Dim ds As DataSet
        If strCustomerCode Is Nothing Then
            ds = DataHelper.ExecuteDataSet2("CheckDuplicateDataForIntUser", strEmail, strLoginName)
        Else
            ds = DataHelper.ExecuteDataSet2("CheckDuplicateDataForCustomer", strCustomerCode, strEmail, strLoginName)
        End If

        Return ds.Tables(0)
    End Function

#End Region

End Class
